package com.hison.tandainbao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hison.tandainbao.entiy.pojo.domain.TbJob;
import com.hison.tandainbao.entiy.pojo.dto.AddJob;
import com.hison.tandainbao.entiy.pojo.dto.JobInfo;
import com.hison.tandainbao.entiy.pojo.dto.UserDTO;
import com.hison.tandainbao.entiy.pojo.dto.UserInfo;
import com.hison.tandainbao.response.ResponseResult;
import com.hison.tandainbao.service.TbJobService;
import com.hison.tandainbao.mapper.TbJobMapper;
import com.hison.tandainbao.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author hison
 * @description 针对表【tb_job】的数据库操作Service实现
 * @createDate 2024-03-30 14:40:27
 */
@Service

public class TbJobServiceImpl extends ServiceImpl<TbJobMapper, TbJob>
        implements TbJobService {

    @Autowired
    private TbJobMapper mapper;

    @Override
    public ResponseResult getAll() {
        List<JobInfo> list = mapper.getAll();
        if (list.size() <= 0) {
            return ResponseResult.error("查询结果为空");
        }
        return ResponseResult.ok(list);
    }

    @Override
    public ResponseResult getJobByName(String jobName) {
        List<JobInfo> jobInfos = mapper.getJobByName(jobName);
        if (jobInfos == null) {
            return ResponseResult.error("查询结构为空");
        }
        return ResponseResult.ok(jobInfos);
    }

    @Override
    public ResponseResult getJobByJobId(String jobId) {
        JobInfo jobInfo = mapper.getJobByJobId(jobId);
        if (jobInfo == null) {
            return ResponseResult.error("查找失败");
        }
        return ResponseResult.ok(jobInfo);
    }

    @Override
    public ResponseResult updateJobStatus(String status, String jobId) {
        int i = mapper.updateJobStatus(jobId, status);
        if (i < 1){
            return  ResponseResult.error("更新失败");
        }
        return ResponseResult.ok("更新成功");
    }

    @Override
    public ResponseResult addJob(AddJob job) {
        job.setAuditStatus(0);
        int i = mapper.insert(job);
        if (i < 1) {
            return ResponseResult.error("添加失败");
        }
        return ResponseResult.ok("添加成功，待管理员审核");
    }

    @Override
    public ResponseResult updateJob(JobInfo jobInfo) {
        int i = mapper.updateJob(jobInfo);
        if (i < 1 ){
            return  ResponseResult.error("修改失败");
        }
        return ResponseResult.ok("修改成功");
    }

    @Override
    public ResponseResult deleteJob(String jobId) {
        int i = mapper.deleteById(jobId);
        if (i < 1 ){
            return  ResponseResult.error("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }

    private static final Logger logger = LoggerFactory.getLogger(TbJobServiceImpl.class);



    @Override
    public ResponseResult getAllByAdmin(int current,  int total) {



        PageHelper.startPage(current,total);
        List<JobInfo> jobInfos = mapper.getAllByAdmin();//将查询结果封装成PageInfo返回给前端，里面包含当前页等很多分页相关信息
        if (jobInfos.size() <= 0){
            return  ResponseResult.error("查找失败");
        }
        return ResponseResult.ok( jobInfos);
    }

    @Override
    public ResponseResult updateJobStatusByAdmin(String jobId, String status) {
        int i = mapper.updateJobStatusByAdmin(jobId,status);
        if(i < 1){
            return  ResponseResult.error("设置失败");
        }
        return ResponseResult.ok("设置成功");
    }

    @Override
    public ResponseResult getMyJobInfoAll() {
        Long id = UserHolder.getUser().getId();
        QueryWrapper<TbJob> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        List<TbJob> tbJobs = mapper.selectList(wrapper);
        if (tbJobs.size() <= 0){
            return  ResponseResult.error("您还未发布职位信息");
        }
        return ResponseResult.ok(tbJobs);
    }
}
